package com.tuananh.radnom.service.config.impl;

import java.util.ArrayList;

import com.tuananh.radnom.model.NetInterface;
import com.tuananh.radnom.model.NetNode;
import com.tuananh.radnom.persist.imp.xml.NetNodeXmlDAO;
import com.tuananh.radnom.service.config.INetNodeService;

public class NetNodeService implements INetNodeService {
	NetNodeXmlDAO nodeXmlDAO;
	PollService pollService;
	public void setPollService(PollService pollService) {
		this.pollService = pollService;
	}

	public void setNodeXmlDAO(NetNodeXmlDAO nodeXmlDAO) {
		this.nodeXmlDAO = nodeXmlDAO;
	}

	public boolean add(NetNode netNode) {
		// TODO Auto-generated method stub
		return nodeXmlDAO.add(netNode);
	}

	public boolean update(NetNode netNode) {
		// TODO Auto-generated method stub
		return nodeXmlDAO.update(netNode);
	}

	public boolean remove(String name) {
		// TODO Auto-generated method stub
		NetNode node = nodeXmlDAO.getByName(name);
		if (nodeXmlDAO.remove(name)){
			System.out.print("remove2");
			// remove all the poll runner that have relation with this interface
			for(NetInterface netInt:node.getNetInterfaces()){
				//pollService.removePollInfo(null, netInt.getName());
			}
			return true;
		}
		return false;
	}

	public ArrayList<NetNode> getAll() {
		// TODO Auto-generated method stub
		System.out.println("Get All");
		return nodeXmlDAO.getAll();
	}

	public NetNode getByName(String name) {
		// TODO Auto-generated method stub
		return nodeXmlDAO.getByName(name);
	}

	public NetNode findByIntName(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	public ArrayList<NetNode> findByIntName(ArrayList<String> names) {
		// TODO Auto-generated method stub
		return null;
	}
	public String getLastedGenerateNodeName(){
		ArrayList<NetNode> listNode = nodeXmlDAO.getAll();
		int maxNumber = 0;
		for(NetNode node:listNode){
			if (node.getName().startsWith("AutoGenerateNode_")){
				int number =  Integer.parseInt(node.getName().substring(17));
				if (number>maxNumber) maxNumber=number;
			}
		}
		return "AutoGenerateNode_"+(maxNumber+1);
	}
	
	public boolean persit() {
		// TODO Auto-generated method stub
		return nodeXmlDAO.persit();
	}

	public boolean merge(NetNode first, NetNode second) {
		// TODO Auto-generated method stub
		return nodeXmlDAO.merge(first, second);
	}

	public NetNode findyByIntIp(String ip) {
		// TODO Auto-generated method stub
		return nodeXmlDAO.findByIntIp(ip);
	}
}
